<template>
  <div class="inner-container">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入文章标题" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="toPublish">发布</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain @click="toPublish" :disabled="selectedIds.length!==1">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain @click="handleDelete" :disabled="!selectedIds.length">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain disabled>导出</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template #default="scope">
          <div style="margin: 1rem 50px">
            <div><b>关键词：</b>{{ scope.row.keywords }}</div>
            <div><b>文章摘要：</b>{{ scope.row.brief }}</div>
            <el-space :size="20" wrap spacer="|">
              <span>顶数量：{{ scope.row.upCount }}</span>
              <span>踩数量：{{ scope.row.downCount }}</span>
              <span>浏览量：{{ scope.row.clickCount }}</span>
              <span>评论量：{{ scope.row.commentCount }}</span>
            </el-space>
          </div>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="50"/>
      <el-table-column prop="coverUrl" label="封面" align="center">
        <template #default="scope">
          <el-image
              :src="scope.row.coverUrl"
              fit="fill"
              :preview-src-list="[scope.row.coverUrl]"
              preview-teleported
              style="width: 64px; height: 64px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="authorName" label="作者" align="center"/>
      <el-table-column prop="categoryName" label="类别" align="center"/>
      <el-table-column prop="title" label="标题" align="center">
        <template #default="scope">
          <el-tooltip placement="top" :content="scope.row.title">
            <el-text line-clamp="2">{{ scope.row.title }}</el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序序号" align="center"/>
      <el-table-column prop="checkState" label="审核状态" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.checkState === 0" type="info">未审核</el-tag>
          <el-tag v-else-if="scope.row.checkState === 1" type="success">通过</el-tag>
          <el-tag v-else-if="scope.row.checkState === 2" type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isDisplay" label="显示状态" align="center">
        <template #default="scope">
          <el-switch
              v-model="scope.row.isDisplay"
              :active-value="1"
              :inactive-value="0"
              @change="handleDisplay(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="isRecommend" label="是否推荐" align="center">
        <template #default="scope">
          <el-switch
              v-model="scope.row.isRecommend"
              :active-value="1"
              :inactive-value="0"
              @change="handleRecommend(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="gmtCreated" label="创建时间" align="center"/>
      <el-table-column label="操作" fixed="right" align="center" width="150">
        <template #default="scope">
          <el-link type="primary" :icon="Checked" @click="showDialog(scope.row)">审核</el-link>
          <el-dropdown>
            <el-link type="primary">更多
              <el-icon class="el-icon--right">
                <arrow-down/>
              </el-icon>
            </el-link>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="toPublish(scope.row)">修改</el-dropdown-item>
                <el-dropdown-item @click="handleDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        background
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item label="文章编号" prop="articleId">
          <el-input v-model="formData.articleId" disabled/>
        </el-form-item>
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="formData.title" disabled/>
        </el-form-item>
        <el-form-item label="审核状态" prop="checkState">
          <el-radio-group v-model="formData.checkState">
            <el-radio :label="0">未审核</el-radio>
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入审核备注" type="textarea" :rows="5"/>
        </el-form-item>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {Checked} from '@element-plus/icons-vue'
import router from '@/router'
import request from '@/utils/request'

// --------------------------------------------------
const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 5})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])

const listData = () => {
  loading.value = true
  request.get('/content/article/list', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total
    tableData.value = res.data.list
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

const handleDelete = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的记录')
    return
  }
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.post(`/content/article/delete`, ids)
        .then(res => {
          ElMessage.success('删除成功')
          selectedIds.value.length = 0
          listData()
        })
  })
}

const handleDisplay = (row) => {
  let operate = row.isDisplay === 0 ? 'display' : 'not-display'
  request.post(`/content/article/${row.id}/${operate}`)
      .then(res => {
        ElMessage.success('操作成功')
      })
}

const handleRecommend = (row) => {
  let operate = row.isRecommend === 0 ? 'recommend' : 'not-recommend'
  request.post(`/content/article/${row.id}/${operate}`)
      .then(res => {
        ElMessage.success('操作成功')
      })
}

const toPublish = (row) => {
  let params = {}
  let id = row.id || selectedIds.value;
  if (id) params.id = id
  router.push({
    query: params,
    path: '/content/article/publish'
  })
}

// --------------------------------------------------
const dialogTitle = ref('文章审核')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const formData = ref({})
const formRules = ref({
  checkState: [
    {required: true, message: '请选择审核状态'}
  ],
  remark: [
    {required: true, message: '请填写审核备注'}
  ]
})

const showDialog = (row) => {
  formData.value.articleId = row.id
  formData.value.title = row.title
  formData.value.checkState = row.checkState
  dialogVisible.value = true
}

const closeDialog = () => {
  formData.value = {
    id: undefined,
    checkState: undefined,
    remark: undefined,
    articleId: undefined,
    title: undefined
  }
  dialogFormRef.value.resetFields()
  dialogVisible.value = false
}

const handleSubmit = () => {
  dialogFormRef.value.validate(valid => {
    if (!valid) {
      ElMessage.error('请正确填写参数')
      return false
    }
    console.log(formData.value)
    request.post(`/content/article/audit`, formData.value)
        .then(res => {
          ElMessage.success('保存成功')
          closeDialog()
          listData()
        })
  })
}

// --------------------------------------------------
onMounted(() => {
  listData()
})
</script>

<style scoped>

</style>
